package com.charity.contoller;

import com.charity.model.CommonResponse;
import com.charity.pojo.Donation;
import com.charity.pojo.Entry;
import com.charity.pojo.User;
import com.charity.service.DonationService;
import com.charity.service.EntryService;
import com.charity.service.UserService;
import com.charity.utils.RSAUtil;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/user")
public class UserController {

    public static final String PubKey = com.charity.utils.IOUtil.readResourceAsString("conf/PubKey");
    public static final String PriKey = com.charity.utils.IOUtil.readResourceAsString("conf/PriKey");

    @Autowired
    private UserService userService;

    @Autowired
    private DonationService donationService;

    @Autowired
    private EntryService entryService;

    private CommonResponse commonResponse = new CommonResponse();

    private RSAUtil rsaUtil = new RSAUtil();

    @RequestMapping("/userById")
    public CommonResponse userById(Long id) throws Exception {
        System.out.println(id);
        User user = userService.userById(id);
        if (user != null) {
            user.setRealName(rsaUtil.decrypt(user.getRealName(), PriKey));
            user.setIdCard(rsaUtil.decrypt(user.getIdCard(), PriKey));
            return commonResponse.ok(user);
        }
        return commonResponse.cuw("300", "没有该用户");
    }

    @PostMapping("/updateUser")
    public CommonResponse updateUser(@RequestBody User user) throws Exception {
        Long id = userService.userId(user.getUserName());
        if (user.getIdCard() == null) {
            user.setIdCard("1");
        }
        if (id != null) {
            Integer result = userService.updateUser(user.getId(), user.getUserName(), user.getPassword(), rsaUtil.encrypt(user.getRealName(), PubKey),
                    user.getSex(), rsaUtil.encrypt(user.getIdCard(), PubKey), user.getAccount(), user.getPhone(), user.getEmail(), user.getAddress(), user.getDifficultyType());
            if (result > 0) {
                return commonResponse.ok(result);
            }
        }
        return commonResponse.cuw("300", "更新失败");
    }

    @PostMapping("/deleteUser")
    public CommonResponse deleteUser(@RequestBody User user) {
        try {


            System.out.println(user.getId());
            Integer result = userService.deleteUser(user.getId());
            if (result > 0) {
                return commonResponse.ok(result);
            }
        }catch (Exception e) {
            return commonResponse.cuw("400", e.getMessage());
        }
        return commonResponse.cuw("300", "删除失败");
    }

    @PostMapping("/addDonation")
    public CommonResponse addDonation(@RequestBody Donation donation) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer result = donationService.addDonation(new Date().getTime() + new Random().nextInt(10), donation.getDonorId(), donation.getDonorName(), donation.getDonationAmount(),
                donation.getEntryId(), sdf.format(new Date()), "审核中");
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "添加失败");
    }

    @PostMapping("suiJiAddDon")
    public CommonResponse suiJiAddDon(@RequestBody Donation donation) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(donation.getDonorId());
        donation.setEntryId(entryService.entryByType());
        Integer result = donationService.addDonation(new Date().getTime() + new Random().nextInt(10), donation.getDonorId(), donation.getDonorName(), donation.getDonationAmount(),
                donation.getEntryId(), sdf.format(new Date()), "审核中");
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "添加失败");
    }

    @GetMapping("/selById")
    public CommonResponse selById(Long id) throws Exception {
        Donation donation = donationService.selById(id);
        if (donation != null) {
            return commonResponse.ok(donation);
        }
        return commonResponse.cuw("300", "没有该捐助单");
    }

    @GetMapping("selByDonorId")
    public CommonResponse selByDonorId(Long donorId) throws Exception {
        List<Donation> donation = donationService.selByDonorId(donorId);
        String donations = JSONArray.toJSONString(donation);
        if (donation != null) {
            return commonResponse.ok(donation);
        }
        return commonResponse.cuw("300", "没有捐助单");
    }

    @PostMapping("/addEntry")
    public CommonResponse addEntry(@RequestBody Entry entry,MultipartFile file) throws Exception {
        System.out.println(entry.getType()+" "+entry.getAccount()+" "+entry.getEntryName()+" "+entry.getRecipientId()+" "+entry.getIntroduction()+" "+
               entry.getTargetAmount());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer result = entryService.addEntry(new Date().getTime() + new Random().nextInt(10), entry.getEntryName(), entry.getRecipientId(), entry.getIntroduction(),
                entry.getType(), entry.getTargetAmount(), sdf.format(new Date()), sdf.format(new Date().getTime()+(7*24*3600*1000)),
                entry.getAccount(),"审核中", 7,file);
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "添加失败");
    }

    @GetMapping("entryByRecId")
    public CommonResponse entryByRecId(Long recipientId) throws Exception {
        List<Entry> entry = entryService.entryByRecId(recipientId);
        String entries = JSONArray.toJSONString(entry);
        if (entries != null) {
            return commonResponse.ok(entry);
        }
        return commonResponse.cuw("300", "没有受助单");
    }

    @PostMapping("/updEntry")
    public CommonResponse updEntry(@RequestBody Entry entry, MultipartFile file) throws Exception {
        Integer result = entryService.updEntry(entry.getId(), entry.getIntroduction(), entry.getType(), entry.getTargetAmount(), "审核中", "", file);
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "更新失败");
    }

    @PostMapping("/updEntryBySQ")
    public CommonResponse updEntryBySQ(Long id,String account) {
        Integer result = entryService.updEntryBySQ(id, account, "审核中");
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "更新失败");
    }

    @PostMapping("/updEntryByBC")
    public CommonResponse updEntryByBC(Long id, String whereAbouts, MultipartFile file) throws Exception {
        Integer result = entryService.updEntryByBC(id, whereAbouts, file);
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "更新失败");
    }

}
